import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';
import { environment } from '../../../environments/environment';
import { LoadingController } from '@ionic/angular';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable()
export class HttpHelperService {
  public API_ROOT_URL;
  private app_environment;
  httpHeaders = new HttpHeaders({
    'Content-Type': 'application/json',
    'Authorization': ''
  });
  httpOptions = {};
  public loader;
  constructor(
    private http: HttpClient,
    private loadingController: LoadingController,
  ) {
    this.app_environment = environment; // 开发环境
    this.API_ROOT_URL = this.app_environment.FRONT_API_ROOT_URL;
  }

  get(url, params?, is_show_loading = false, is_show_message = false, options?): Observable<any> {
    url = this.API_ROOT_URL + url;
    options = { ...this.httpOptions, ...options };
    const loading = this.loadingToast(is_show_loading);
    return this.http.get(url, { params, ...options }).pipe(
      tap(
        this.tapHandle(is_show_loading, is_show_message, loading),
        error => {
          if (is_show_loading === true) {
            // loading.then(load=>load.dismiss());
          }
        }),
      // // catchError(this.handleError(url, {}))
    );
  }

  // post(url, params?, is_show_loading = false, is_show_message = false, options?): Observable<any> {
  //   url = this.getUrl(url, this.connection);
  //   options = { ...this.httpOptions, ...options };
  //   const toast_id = this.loadingToast(is_show_loading);
  //   return this.http.post(url, params, options).pipe(
  //     tap(this.tapHandle(is_show_loading, is_show_message, toast_id),
  //       error => {
  //         if (is_show_loading === true) {
  //           this.nzMessageService.remove(toast_id);
  //         }
  //       }),
  //     catchError(this.handleError(url, {}))
  //   );
  // }

  // put(url, params?, is_show_loading = false, is_show_message = false, options?): Observable<any> {
  //   url = this.getUrl(url, this.connection);
  //   options = { ...this.httpOptions, ...options };
  //   const toast_id = this.loadingToast(is_show_loading);
  //   return this.http.put(url, params, options).pipe(
  //     tap(this.tapHandle(is_show_loading, is_show_message, toast_id),
  //       error => {
  //         if (is_show_loading === true) {
  //           this.nzMessageService.remove(toast_id);
  //         }
  //       }),
  //     catchError(this.handleError(url, {}))
  //   );
  // }

  // delete(url, params?, is_show_loading = false, is_show_message = false, options?): Observable<any> {
  //   url = this.getUrl(url, this.connection);
  //   options = { ...this.httpOptions, ...options };
  //   const toast_id = this.loadingToast(is_show_loading);
  //   return this.http.delete(url, { params, ...options }).pipe(
  //     tap(this.tapHandle(is_show_loading, is_show_message, toast_id),
  //       error => {
  //         if (is_show_loading === true) {
  //           this.nzMessageService.remove(toast_id);
  //         }
  //       }),
  //     catchError(this.handleError(url, {}))
  //   );
  // }

  // // url
  // private getUrl(url, connection) {
  //   if (connection === 'Front') {
  //     this.API_ROOT_URL = environment.FRONT_API_ROOT_URL;
  //   } else if (connection === 'Back') {
  //     this.API_ROOT_URL = environment.BACK_API_ROOT_URL;
  //     this.httpOptions = { withCredentials: true, headers: this.httpHeaders };
  //   } else if (connection === 'Self') {
  //     this.API_ROOT_URL = '';
  //   }
  //   return this.API_ROOT_URL + url;
  // }

  // loading-toast
  private async loadingToast(is_show_loading) {
    let loading = null;
    if (is_show_loading) {
      loading = await this.loadingController.create({
        spinner: 'dots',
        message: '请求中...',
      });
      loading.present();
    }
    return loading;
  }

  // tap窥探
  private tapHandle(is_show_loading, is_show_message, loading) {
    return (res: any) => {
      if (is_show_loading === true) {
        loading.then(load => load.dismiss());
      }
      if (is_show_message === true) {
        if (res.code === 1) {

        } else {
        }
      }
    };
  }

  // // 错误捕捉
  // private handleError<T>(url = '未知地址', result?: T) {
  //   return (err: HttpErrorResponse): Observable<T> => {
  //     // console.error(err);
  //     if (err.error instanceof Error) {
  //       this.nzMessageService.error('内部错误:' + err.error.message);
  //     } else {
  //       if (err.error.code === 0) {
  //         // this.nzMessageService.info('请重新登录后访问！');
  //         // this.router.navigate(['/auth/login']);
  //       } else if (err.error.code !== null) {
  //         this.nzMessageService.error('Code: ' + err.error.code + '  Message: ' + err.error.message);
  //       } else {
  //         this.nzMessageService.error('系统或网络造成接口请求错误，请稍后重试！');
  //       }
  //     }
  //     // return throwError('系统或网络造成接口请求错误，请稍后重试！'); // subscribe无法继续
  //     return of(result as T); // subscribe继续
  //   };
  // }

}
